﻿using Core.Rendering.Resources;
using Core.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Core.ECS.Components
{
    [DataContract]
    public class CModelRenderer : IComponent
    {
        public Guid Id
        {
            get => _id; set => _id = value;
        }
        [DataMember] private Guid _id;
        public Model? Model { get; private set; }
        public string ModelPath
        {
            get => _modelPath;
            set
            {
                _modelPath = value;
                if (Scene.Models.ContainsKey(value))
                {
                    Model = Scene.Models[value];
                }
                else
                {
                    Model = Model.Create(value);
                    Scene.Models.Add(value, Model);
                }
            }
        }
        public void SetModelPath(string modelPath)
        {
            _modelPath = modelPath;
        }
        public void SetMaterialPath(string modelPath)
        {
            _materialPath = modelPath;
        }
        public Material? Material { get; set; }

        [DataMember] private string _modelPath;
        public string MaterialPath
        {
            get => _materialPath;
            set
            {
                _materialPath = value;
                if (Scene.Materials.ContainsKey(value))
                {
                    Material = Scene.Materials[value];
                }
                else
                {
                    SerializeHelper.Deserialize(value, out Material material);
                    if (material != null)
                    {
                        material.Start();
                        Material = material;
                    }
                    Scene.Materials.Add(value, material);
                }
            }
        }
        [DataMember] private string _materialPath;
        public CModelRenderer(Guid id,
            string modelPath = "./resources/objects/Models/Cube.fbx",
            string materialPath = "./resources/material.xml")//后面添加上默认model和material的路径
        {
            _id = id;
            _modelPath = modelPath;
            _materialPath = materialPath;
        }
        public void Start()
        {
            if(Scene.Models.ContainsKey(ModelPath))
            {
                Model = Scene.Models[ModelPath];
            }
            else
            {
                Model = Model.Create(ModelPath);
                Scene.Models.Add(ModelPath, Model);
            }
            if (Scene.Materials.ContainsKey(MaterialPath))
            {
                Material = Scene.Materials[MaterialPath];
            }
            else
            {
                SerializeHelper.Deserialize(_materialPath, out Material material);
                if (material != null)
                {
                    material.Start();
                    Material = material;
                    Scene.Materials.Add(MaterialPath, Material);
                }
            }
        }
    }
}
